Walkthrough 10-4: Handle errors at the flow level

In this walkthrough, you add multiple error handlers to a flow. You will:

·       Add error handlers to a flow.

·       Test the behavior of errors thrown in a flow and by a child flow.

·       Compare On Error Propagate and On Error Continue scopes in a flow.

·       Set an HTTP status code in an error handler and modify an HTTP Listener to return it.

 

Starting file

If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.

Add an On Error Propagate error handler to a flow

1.     Return to implementation.xml.

2.     Expand the Error handling section of the getFlights flow.

3.     Add an On Error Propagate scope.

4.     Set the error type to ANY so it will initially catch both the validation and American flights errors.

 

Set the payload in the error handler to the error description

5.     Add a Transform Message component to the scope and set the display name to No flights.

6.     In the Transform Message properties view, set the payload to a JSON message property equal to the string No flights to and concatenate the code variable.

7.     Coerce the variable to a String.

Set the HTTP status code in the error handler to 200

8.     Add a Set Variable transformer to the On Error Propagate.

9.     In the Set Variable properties view, set the display name and name to httpStatus.

10.  Set the value to 200.

 

Set the HTTP Listener error response status code to use a variable

11.  Navigate to the Responses tab in the properties view for the GET /flights Listener.

12.  Use expression mode to change the error response status code (not the response status code!) to an expression that sets it equal to the value of an httpStatus variable with a default value of 500.

vars.httpStatus default 500

 

Test the flow’s On Error Propagate with an error in the flow

13.  Save the file, debug the project, and proceed through any errors in the workspace.

14.  In Advanced REST Client, change the airline to make a request to http://localhost:8081/flights?airline=american&code=FOO.

15.  In the Mule Debugger, step until the event is passed to the flow’s error handler.

16.  Step through the rest of the application.

17.  Return to Advanced REST Client; you should get the 200 status code and the JSON message.

Test the flow’s On Error Propagate with an error in a child flow

18.  In Advanced REST Client, change the code to make a request to http://localhost:8081/flights?airline=american&code=PDX.

19.  Step until the event is passed to the globalError_Handler.

20.  Step until the error is passed to the parent flow’s error handler.

21.  Step through the rest of the application.

22.  Return to Advanced REST Client; you should get the 200 status code and the JSON error message.

23.  Return to Anypoint Studio and switch to the Mule design perspective.

Specify the type of error to be handled by the flow’s error handler

24.  Navigate to the properties view for the On Error Propagate in getFlights.

25.  Change the type from ANY to AMERICAN-FLIGHTS-API:BAD_REQUEST.

Test the application

26.  Save the file to redeploy the project.

27.  In Advanced REST Client, make another request to http://localhost:8081/flights?airline=american&code=PDX.

28.  In the Mule Debugger, step through the application; the error should still be handled by globalError_Handler and then the getFlights flow error handler.

29.  In Advanced REST Client, change the code to make a request to http://localhost:8081/flights?airline=american&code=FOO.

30.  In the Mule Debugger, step through the application; you should get a 500 Server Error and no message because the error is not handled by the getFlights flow error handler or by globalError_handler.

31.  Return to Anypoint Studio and switch to the Mule Design perspective.

32.  Stop the project.

33.  Navigate to the Responses tab in the properties view for the GET /flights Listener.

34.  Review the error response body (not the response body!) and ensure you know why you got the last response for http://localhost:8081/flights?airline=american&code=FOO.

Use an On Error Continue error handler in a flow

35.  Add an On Error Continue scope to getFlights.  

36.  In the On Error Continue properties view, set the type to VALIDATION:INVALID_BOOLEAN.

37.  Add a Transform Message component to the scope and set the display name to error.description.

38.  In the Transform Message properties view, set the payload to a JSON message property equal to the error.description.

39.  Add a Set Variable transformer to the On Error Continue scope.

40.  Set the display name and name to httpStatus and set the value to 400.

 

Test the application

41.  Save the file, run the project, and proceed through any errors in the workspace.

42.  In Advanced REST Client, make another request to http://localhost:8081/flights?airline=american&code=FOO; you should get a 200 response and the invalid destination message again – not the 400 response.

Set the HTTP Listener response status code

43.  Return to Anypoint Studio.

44.  Navigate to the Responses tab in the properties view for the GET /flights Listener.

45.  Use expression mode to set the response status code (not the error response status code!) to an expression that sets it equal to the value of an httpStatus variable with a default value of 200.

vars.httpStatus default 200

Test the application

46.  Save the file to redeploy the project.

47.  In Advanced REST Client, make another request to http://localhost:8081/flights?airline=american&code=FOO; you should now get the 400 response.

Test the application to see what happens with the Scatter-Gather

48.  In Advanced REST Client, change the code and make a request to http://localhost:8081/flights?airline=american&code=PDX; you should get a 200 response and no flights to PDX.

49.  Change the airline to make a request to http://localhost:8081/flights?airline=united&code=PDX; you should get United flights to PDX.

 

50.  Remove the airline to make a request to http://localhost:8081/flights?code=PDX; you should get flights to PDX because United has flights, but you get none.

 

51.  Return to Anypoint Studio.

52.  Stop the project.



Did you complete the walkthrough?

  Yes, I completed the walkthrough

  No, I did not complete the walkthrough

  I completed part of the walkthrough


Comments and/or feedback